﻿/*======================================================================
== Copyright : BlueCurve (c)
== Licence   : Gnu/GPL v2.x
== Author    : Teddy Albina
== Email     : bluecurveteam@gmail.com
== Web site  : http://www.codeplex.com/BlueCurve
========================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace BlueCurve.Common.Share
{
    /// <summary>
    /// Fournit les méthodes natives pour l'interaction avec les partages de fichiers
    /// </summary>
    internal class NativeMethods
    {
        private NativeMethods()
        {
        }

        public const int NO_ERROR = 0;

        /// <summary>
        /// Flags utilisés pour <see cref="WNetAddConnection3"/>.
        /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wnet/wnet/wnetaddconnection3.asp
        /// </summary>
        [Flags]
        public enum WNetAddConnectionFlags : uint
        {
            /// <summary>
            /// If this flag is set, the operating system may interact with the user for authentication purposes.
            /// </summary>
            CONNECT_INTERACTIVE = 0x00000008,

            /// <summary>
            /// This flag instructs the system not to use any default settings for user names or passwords without offering the user the opportunity to supply an alternative. This flag is ignored unless CONNECT_INTERACTIVE is also set.
            /// </summary>
            CONNECT_PROMPT = 0x00000010,

            /// <summary>
            /// This flag forces the redirection of a local device when making the connection. 
            /// If the lpLocalName member of NETRESOURCE specifies a local device to redirect, this flag has no effect, because the operating system still attempts to redirect the specified device. When the operating system automatically chooses a local device, the dwType member must not be equal to RESOURCETYPE_ANY.
            /// If this flag is not set, a local device is automatically chosen for redirection only if the network requires a local device to be redirected.
            /// Windows Server 2003 and Windows XP:  When the system automatically assigns network drive letters, letters are assigned beginning with Z:, then Y:, and ending with C:. This reduces collision between per-logon drive letters (such as network drive letters) and global drive letters (such as disk drives). Note that previous releases assigned drive letters beginning with C: and ending with Z:.
            /// </summary>
            CONNECT_REDIRECT = 0x00000080,

            /// <summary>
            /// The network resource connection should be remembered. 
            /// If this bit flag is set, the operating system automatically attempts to restore the connection when the user logs on.
            /// The operating system remembers only successful connections that redirect local devices. It does not remember connections that are unsuccessful or deviceless connections. (A deviceless connection occurs when the lpLocalName member is NULL or when it points to an empty string.)
            /// If this bit flag is clear, the operating system does not automatically restore the connection at logon.
            /// </summary>
            CONNECT_UPDATE_PROFILE = 0x00000001,

            /// <summary>
            /// If this flag is set, the operating system prompts the user for authentication using the command line instead of a graphical user interface (GUI). This flag is ignored unless CONNECT_INTERACTIVE is also set.
            /// Windows 2000/NT and Windows Me/98/95:  This value is not supported.
            /// </summary>
            CONNECT_COMMANDLINE = 0x00000800,

            /// <summary>
            /// If this flag is set, and the operating system prompts for a credential, the credential should be saved by the credential manager. If the credential manager is disabled for the caller's logon session, or if the network provider does not support saving credentials, this flag is ignored. This flag is also ignored unless you set the CONNECT_COMMANDLINE flag.
            /// Windows 2000/NT and Windows Me/98/95:  This value is not supported.
            /// </summary>
            CONNECT_CMD_SAVECRED = 0x00001000
        }

        /// <summary>
        /// Flags utilisés pour <see cref="WNetCancelConnection2"/>.
        /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wnet/wnet/wnetcancelconnection2.asp
        /// </summary>
        public enum WNetCancelConnectionFlags : uint
        {
            /// <summary>
            /// The system does not update information about the connection. 
            /// If the connection was marked as persistent in the registry, the system continues to restore the connection at the next logon. If the connection was not marked as persistent, the function ignores the setting of the CONNECT_UPDATE_PROFILE flag.
            /// </summary>
            ZERO = 0,

            /// <summary>
            /// The system updates the user profile with the information that the connection is no longer a persistent one. 
            /// The system will not restore this connection during subsequent logon operations. (Disconnecting resources using remote names has no effect on persistent connections.)
            /// </summary>
            CONNECT_UPDATE_PROFILE = 0x00000001
        }

        /// <summary>
        /// The WNetAddConnection3 function makes a connection to a network resource. The function can redirect a local device to the network resource.
        /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wnet/wnet/wnetaddconnection3.asp
        /// </summary>
        /// <param name="hwndOwner">[in] Handle to a window that the provider of network resources can use as an owner window for dialog boxes. Use this parameter if you set the CONNECT_INTERACTIVE value in the dwFlags parameter. 
        /// The hwndOwner parameter can be NULL. If it is, a call to WNetAddConnection3 is equivalent to calling the WNetAddConnection2 function.
        /// </param>
        /// <param name="lpNetResource">[in] Pointer to a NETRESOURCE structure that specifies details of the proposed connection, such as information about the network resource, the local device, and the network resource provider. 
        /// You must specify the following members of the NETRESOURCE structure.
        /// - dwType
        /// - lpLocalName
        /// - lpRemoteName
        /// - lpProvider
        /// The WNetAddConnection3 function ignores the other members of the NETRESOURCE structure.
        /// </param>
        /// <param name="lpPassword">[in] Pointer to a null-terminated string that specifies a password to be used in making the network connection. 
        /// If lpPassword is NULL, the function uses the current default password associated with the user specified by the lpUserName parameter. 
        /// If lpPassword points to an empty string, the function does not use a password.
        /// If the connection fails because of an invalid password and the CONNECT_INTERACTIVE value is set in the dwFlags parameter, the function displays a dialog box asking the user to type the password.
        /// Windows Me/98/95:  This parameter must be NULL or an empty string.
        /// </param>
        /// <param name="lpUsername">[in] Pointer to a null-terminated string that specifies a user name for making the connection. 
        /// If lpUserName is NULL, the function uses the default user name. (The user context for the process provides the default user name.)
        /// The lpUserName parameter is specified when users want to connect to a network resource for which they have been assigned a user name or account other than the default user name or account.
        /// The user-name string represents a security context. It may be specific to a network provider.
        /// Windows Me/98/95:  This parameter must be NULL or an empty string.
        /// </param>
        /// <param name="dwFlags">[in] Connection options. The following values are currently defined.</param>
        /// <returns>If the function succeeds, the return value is <see cref="NO_ERROR"/>.</returns>
        [DllImport("mpr.dll")]
        public static extern uint WNetAddConnection3(
            [In]IntPtr hwndOwner,
            [In]ref NETRESOURCE lpNetResource,
            [In]string lpPassword,
            [In]string lpUsername,
            [In]WNetAddConnectionFlags dwFlags
            );

        /// <summary>
        /// The WNetCancelConnection2 function cancels an existing network connection. You can also call the function to remove remembered network connections that are not currently connected.
        /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wnet/wnet/wnetcancelconnection2.asp
        /// </summary>
        /// <param name="lpName">[in] Pointer to a constant null-terminated string that specifies the name of either the redirected local device or the remote network resource to disconnect from. 
        /// If this parameter specifies a redirected local device, the function cancels only the specified device redirection. If the parameter specifies a remote network resource, all connections without devices are canceled.
        /// </param>
        /// <param name="dwFlags">[in] Connection type. The following values are defined.</param>
        /// <param name="fForce">[in] Specifies whether the disconnection should occur if there are open files or jobs on the connection. If this parameter is FALSE, the function fails if there are open files or jobs.</param>
        /// <returns>If the function succeeds, the return value is <see cref="NO_ERROR"/>.</returns>
        [DllImport("mpr.dll")]
        public static extern uint WNetCancelConnection2(
            [In]string lpName,
            [In]WNetCancelConnectionFlags dwFlags,
            [In]bool fForce
            );
    }
}
